<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title></title>

  <style type="text/css">
    * {
      padding: 0;
      margin: 0;
      list-style: none;
      border: 0;
    }

    .all {
      width: 500px;
      height: 200px;
      padding: 2px;
      border: 1px solid #ccc;
      margin: 100px auto;
      position: relative;
    }

    .screen {
      width: 500px;
      height: 200px;
      overflow: hidden;
      position: relative;
    }

    .screen li {
      width: 500px;
      height: 200px;
      overflow: hidden;
      float: left;
    }

    .screen ul {
      position: absolute;
      left: 0;
      top: 0px;
      width: 3000px;
    }

    .all ol {
      position: absolute;
      right: 10px;
      bottom: 10px;
      line-height: 20px;
      text-align: center;
    }

    .all ol li {
      float: left;
      width: 20px;
      height: 20px;
      background: #fff;
      border: 1px solid #ccc;
      margin-left: 10px;
      cursor: pointer;
    }

    .all ol li.current {
      background: yellow;
    }

    #arr {
      display: none;
      z-index: 1000;

    }

    #arr span {
      width: 40px;
      height: 40px;
      position: absolute;
      left: 5px;
      top: 50%;
      margin-top: -20px;
      background: #000;
      cursor: pointer;
      line-height: 40px;
      text-align: center;
      font-weight: bold;
      font-family: '黑体';
      font-size: 30px;
      color: #fff;
      opacity: 0.3;
      border: 1px solid #fff;
    }

    #arr #right {
      right: 5px;
      left: auto;
    }
  </style>
  <script src="./js/animate.js"></script>
</head>

<body>
  <div class="all" id='box'>
    <div class="screen">
      <ul class="uls">
        <li><img src="img/wf1.jpg" width="500" height="200" /></li>
        <li><img src="img/wf2.jpg" width="500" height="200" /></li>
        <li><img src="img/wf3.jpg" width="500" height="200" /></li>
        <li><img src="img/wf4.jpg" width="500" height="200" /></li>
        <li><img src="img/wf5.jpg" width="500" height="200" /></li>
      </ul>
      <ol class="ols">
      </ol>
    </div>
    <div id="arr"><span id="left">&lt;</span><span id="right">&gt;</span></div>
  </div>

</body>

<script>
  var all = document.querySelector('.all')
  var arr = document.querySelector('#arr')
  var uls = document.querySelector('.uls')
  var lis = uls.querySelectorAll('li')
  var liWidth = lis[0].offsetWidth
  var ols = document.querySelector('.ols')
  var leftSpan = arr.querySelector('#left')
  var rightSpan = arr.querySelector('#right')
  all.addEventListener('mouseenter', function () {
    arr.style.display = 'block'
    clearInterval(timer)
    timer = null
  })
  all.addEventListener('mouseleave', function () {
    arr.style.display = 'none'
    timer = setInterval(function () {
      rightSpan.click()
    }, 2000)

  })
  //1.动态生成小圆点,点击小圆点移动图片

  for (var i = 0; i < lis.length; i++) {
    var oli = document.createElement('li')
    oli.setAttribute('index', i)
    oli.innerHTML = i + 1
    ols.appendChild(oli)

    oli.addEventListener('click', function () {

      for (var j = 0; j < ols.children.length; j++) {
        ols.children[j].className = ''
      }
      this.className = 'current'
      var oliIndex = this.getAttribute('index')
      num = oliIndex
      circle = oliIndex

      animate(uls, -oliIndex * liWidth)
    })

  }
  ols.children[0].className = 'current'

  //给右箭头添加点击事件
  //复制第一张图片放在最后
  var first = uls.children[0].cloneNode(true)
  uls.appendChild(first)
  var num = 0;
  var circle = 0
  var flag = true
  rightSpan.addEventListener('click', function () {

    if (flag) {
      flag = false
      if (num == uls.children.length - 1) {
        uls.style.left = 0;
        num = 0
      }
      num++
      animate(uls, -num * liWidth, function () {
        flag = true
      })
      circle++

      if (circle == ols.children.length) {
        circle = 0
      }

      for (var i = 0; i < ols.children.length; i++) {
        ols.children[i].className = ''

      }
      ols.children[circle].className = 'current'
    }

  })

  leftSpan.addEventListener('click', function () {
    if (flag) {
      flag = false
      if (num == 0) {
        num = uls.children.length - 1
        uls.style.left = -num * liWidth + 'px';

      }
      num--
      animate(uls, -num * liWidth, function () {
        flag = true
      })
      circle--

      if (circle < 0) {
        circle = ols.children.length - 1
      }

      for (var i = 0; i < ols.children.length; i++) {
        ols.children[i].className = ''

      }
      ols.children[circle].className = 'current'
    }
  })
  //自动播放
  var timer = setInterval(function () {
    rightSpan.click()
  }, 2000)
</script>

</html>